Display a menu bar in a .NET MAUI desktop app

您所在的位置:网站首页 sound blaster command app Display a menu bar in a .NET MAUI desktop app

Display a menu bar in a .NET MAUI desktop app

#Display a menu bar in a .NET MAUI desktop app| 来源: 网络整理| 查看: 265

Display a menu bar in a .NET MAUI desktop app Article 05/05/2023

A .NET Multi-platform App UI (.NET MAUI) menu bar is a container that presents a set of menus in a horizontal row, at the top of an app on Mac Catalyst and Windows.

Each top-level menu in the menu bar, known as a menu bar item, is represented by a MenuBarItem object. MenuBarItem defines the following properties:

Text, of type string, defines the menu text. IsEnabled, of type boolean, specifies whether the menu is enabled. The default value of this property is true.

These properties are backed by BindableProperty objects, which means that they can be targets of data bindings, and styled.

A MenuBarItem can consist of the following children:

MenuFlyoutItem, which represents a menu item that can be clicked. MenuFlyoutSubItem, which represents a sub-menu item that can be clicked. MenuFlyoutSeparator, which is a horizontal line that separates items in the menu.

MenuFlyoutSubItem derives from MenuFlyoutItem, which in turn derives from MenuItem. MenuItem defines multiple properties that enable the appearance and behavior of a menu item to be specified. The appearance of a menu item, or sub-item, can be defined by setting the Text, and IconImageSource properties. The response to a menu item, or sub-item, click can be defined by setting the Clicked, Command, and CommandParameter properties.

Create menu bar items

MenuBarItem objects can be added to the MenuBarItems collection, of type IList, on a ContentPage. .NET MAUI desktop apps will display a menu bar, containing menu items, when they are added to any ContentPage that's hosted in a NavigationPage or a Shell app.

The following example shows a ContentPage that defines menu bar items:

This example defines three top-level menus. Each top-level menu has menu items, and the second top-level menu has a sub-menu and a separator:

In this example, each MenuFlyoutItem defines a menu item that executes an ICommand when selected.

Display icons on menu items

MenuFlyoutItem and MenuFlyoutSubItem inherit the IconImageSource property from MenuItem, which enables a small icon to be displayed next to the text for a menu item. This icon can either be an image, or a font icon.

Warning

Mac Catalyst does not support displaying icons on menu items.

The following example shows a menu bar item, where the icons for menu items are defined using font icons:

In this example, the menu bar item defines three menu items that display an icon and text on Windows.

For more information about displaying font icons, see Display font icons. For information about adding images to .NET MAUI projects, see Add images to a .NET MAUI app project.

Mac Catalyst limitations

.NET MAUI Mac Catalyst apps are limited to 50 menu items. Attempting to add more than 50 menu items to a Mac Catalyst app will result in an exception being thrown.

Additional menu items, beyond the 50 limit, can be added to a menu bar by adding the following code to your AppDelegate class:

[Export("MenuItem50: ")] internal void MenuItem50(UICommand uICommand) { uICommand.SendClicked(); }


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3